var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
define(["require", "exports", "../class/SVGGraphics", "../class/Tween", "./common"], function (require, exports, SVGGraphics_1, Tween_1, common_1) {
    "use strict";
    var FoodEffect = (function () {
        function FoodEffect(config) {
            this.add_body = 0;
            this.add_speed = 0;
            this.add_sensitivity = 0;
            config && this.importConfig(config);
        }
        FoodEffect.prototype.importConfig = function (config) {
            this.add_body = config.add_body;
            this.add_speed = config.add_speed;
            this.add_sensitivity = config.add_sensitivity;
        };
        return FoodEffect;
    }());
    exports.FoodEffect = FoodEffect;
    var Food = (function (_super) {
        __extends(Food, _super);
        function Food(tex) {
            var _this = this;
            _super.call(this, tex);
            this.name = "无用食物";
            this._ANI = new Tween_1.default;
            /**是否无视碰撞检测：在被吃后，物体动画中，这时候无需有任何作用，只需要把动画跑完就行了 */
            this.is_ignore = false;
            this.effect = new FoodEffect;
            var self = this;
            this.on("eated", function () {
                _this.is_ignore = true;
                // this._ANI.Tween("fade out", { alpha: 1, scale: 1 }, true)
                //     .to({
                //         alpha: 0,
                //         scale: 0
                //     }, L_ANI_TIME)
                //     .easing(TWEEM.Easing.Quartic.In)
                //     .onUpdate(function () {
                //         self.alpha = this.alpha
                //         self.scale = this.scale
                //     })
                //     .onComplete(() => {
                //         this.parent.removeChild(this)
                //         this.destroy();
                //     })
                //     .start()
                _this.parent.removeChild(_this);
                _this.destroy();
            });
        }
        Food.prototype.onEat = function (snake) {
            this.emit("eated");
            this.effectFood(snake);
        };
        /**触发食物功效 */
        Food.prototype.effectFood = function (snake) {
            var food_effect = this.effect;
            if (food_effect.add_body) {
                snake.addBody(food_effect.add_body);
            }
            if (food_effect.add_speed) {
                snake.addSpeed(food_effect.add_speed);
            }
            if (food_effect.add_sensitivity) {
                snake.addSensitivity(food_effect.add_sensitivity);
            }
        };
        Food.prototype.update = function (time) {
            this._ANI.update(time);
        };
        Food.prototype.destroy = function () {
            PIXI.Container.prototype.destroy.call(this);
            this._ANI = null;
        };
        return Food;
    }(PIXI.Sprite));
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = Food;
    exports.FOOD_CONTAINER_UNIT_SIZE = 600;
    var Emitter = PIXI.particles.Emitter;
    ;
    var FoodContainer = (function (_super) {
        __extends(FoodContainer, _super);
        function FoodContainer() {
            var _this = this;
            _super.call(this);
            this._ANI = new Tween_1.default;
            /**上一次渲染light的index集合 */
            this._pre_ligth_render_map = {};
            this._particle_foods = [];
            this._show_particle_foods = [];
            this._emitters = [];
            this._lights = [];
            this._lightsContainer = (function () {
                var con = new PIXI.Container();
                _this.addChild(con);
                return con;
            })();
            // removeParticleFoodAt(index: number) {
            //     return this._particle_foods.splice(index, 1)[0]
            // }
            /**已经移除的emitter，这里在执行最后的消失动画 */
            this._death_emitters = [];
            this._death_lights = [];
            // updatePos(x: number, y: number, overflow: number = 10) {
            //     this.x = x;
            //     this.y = y;
            //     var emitter = this._emitter
            //     var spawnRect = emitter.spawnRect;
            //     var width = spawnRect.width + overflow
            //     var height = spawnRect.height + overflow
            //     this._show_particle_foods = this._particle_foods.filter((item) => {
            //         var _x = item.x + x
            //         var _y = item.y + y
            //         return _x > -overflow && _x < width && _y > -overflow && _y < height
            //     });
            // }
            this.show_emitter_range_left = 0;
            this.show_emitter_range_right = 0;
            this.show_emitter_range_top = 0;
            this.show_emitter_range_bottom = 0;
        }
        FoodContainer.prototype.addFood = function (food, x, y) {
            food.x = x;
            food.y = y;
            this.addChild(food);
        };
        FoodContainer.prototype.getCollisionItemWithCircle = function (x, y, r, filter) {
            if (r === void 0) { r = 0; }
            var children_indexs = [];
            // this.children.forEach((item: PIXI.DisplayObject, index) => {
            //     if (item instanceof Food || item instanceof Emitter) {
            //         if (filter(item, x, y, index)) {
            //             children_indexs.push(index)
            //         }
            //     }
            // })
            var emiter_pos_indexs = [];
            this._particle_foods.forEach(function (item, index) {
                if (filter(item, x, y, index)) {
                    emiter_pos_indexs.push(index);
                }
            });
            var res = {
                children_indexs: children_indexs,
                emiter_pos_indexs: emiter_pos_indexs
            };
            return res;
        };
        FoodContainer.prototype.update = function (time) {
            var _this = this;
            this._ANI.update();
            var children = this.children;
            for (var _a = 0, children_1 = children; _a < children_1.length; _a++) {
                var food = children_1[_a];
                if (food instanceof Food) {
                    food.update(time);
                }
                else if (food instanceof PIXI.particles.Emitter) {
                }
            }
            var _now = Date.now();
            var _elapsed = this._elapsed || (_now - 16);
            var delay_time = (_now - _elapsed) * 0.001;
            this._elapsed = _now;
            // var emitter = this._emitter;
            // if (emitter && this._particle_foods.length) {
            //     emitter.update(delay_time);
            // }
            var _pre_ligth_render_map = this._pre_ligth_render_map;
            var lights = this._lights;
            var _emitters = this._emitters;
            var _lightsContainer = this._lightsContainer;
            if (_emitters && _emitters.length) {
                _emitters.forEach(function (emitter, i) {
                    if (emitter.ownerPos.x >= _this.show_emitter_range_left && emitter.ownerPos.x <= _this.show_emitter_range_right &&
                        emitter.ownerPos.y >= _this.show_emitter_range_top && emitter.ownerPos.y <= _this.show_emitter_range_bottom) {
                        emitter.update(delay_time);
                        if (!_pre_ligth_render_map[i]) {
                            _pre_ligth_render_map[i] = true;
                            _lightsContainer.addChild(lights[i]);
                        }
                    }
                    else {
                        if (_pre_ligth_render_map[i]) {
                            _pre_ligth_render_map[i] = false;
                            _lightsContainer.removeChild(lights[i]);
                        }
                    }
                });
            }
            var death_emitters = this._death_emitters;
            if (death_emitters && death_emitters.length) {
                death_emitters.forEach(function (emitter) {
                    emitter.update(delay_time);
                    return emitter.emit;
                });
            }
        };
        FoodContainer.prototype.addParticleFood = function (effect, x, y) {
            var add_body = effect.add_body;
            var add_speed = effect.add_speed;
            var add_sensitivity = effect.add_sensitivity;
            var add_total = add_body + add_speed + add_sensitivity;
            if (!add_total) {
                return;
            }
            var random_color = add_total.toFixed(9).match(/\d\d\d/g);
            var endScale = (add_body / 10 + add_speed / 20 + add_sensitivity / 50);
            var startScale = endScale * 1.2 + 0.3;
            var darkColor = [
                parseInt(random_color[0]) / 999 * 127.5 + 127.5,
                parseInt(random_color[1]) / 999 * 100 + 155,
                parseInt(random_color[2]) / 999 * 55 + 200,
            ];
            var lightColor = [
                darkColor[0] * 0.8,
                darkColor[1] * 0.8,
                darkColor[2] * 0.8,
            ];
            var number_color = darkColor[0] * 0x10000 + darkColor[1] * 0x100 + darkColor[2];
            // var light_wrap = new PIXI.Container();
            var light = new PIXI.Sprite(FoodTexture);
            light.anchor.set(0.5, 0.5);
            light.scale.set(startScale * 5, startScale * 5);
            light.alpha = endScale;
            light.tint = number_color;
            light.blendMode = PIXI.BLEND_MODES.ADD;
            light.x = x;
            light.y = y;
            // light_wrap.addChild(light);
            var emitter = new Emitter(this, '/images/food_particle.png', {
                "alpha": {
                    "start": 1,
                    "end": 1
                },
                "scale": {
                    "start": startScale * 0.2,
                    "end": startScale * 0.2,
                    "minimumScaleMultiplier": 0
                },
                "color": {
                    "start": "54d7ff",
                    "end": "50eeff"
                },
                "speed": {
                    "start": 10 * startScale,
                    "end": 0
                },
                "acceleration": {
                    "x": 0,
                    "y": 0
                },
                "startRotation": {
                    "min": 0,
                    "max": 360
                },
                "noRotation": false,
                "rotationSpeed": {
                    "min": 0,
                    "max": 0
                },
                "lifetime": {
                    "min": 0.5,
                    "max": 1
                },
                "blendMode": "add",
                "frequency": 0.01,
                "emitterLifetime": -1,
                "maxParticles": 1,
                "pos": {
                    "x": 0,
                    "y": 0
                },
                "addAtBack": false,
                "spawnType": "point",
            });
            emitter.startColor = darkColor;
            emitter.endColor = lightColor;
            emitter.emit = true;
            emitter.resetPositionTracking();
            emitter.updateOwnerPos(x, y);
            this._particle_foods.push({ x: x, y: y, effect: effect });
            this._emitters.push(emitter);
            this._lights.push(light);
        };
        FoodContainer.prototype.removeParticleFoodAt = function (index, options) {
            if (options === void 0) { options = {}; }
            var death_emitters = this._death_emitters;
            var death_emitter = this._emitters.splice(index, 1)[0];
            var die_time = 300;
            function remove_deatch_emitter() {
                // var index = death_emitters.indexOf(death_emitter);
                // if (index > -1) {
                //     death_emitters.splice(index, 1);
                //     death_emitter.destroy();
                // }
            }
            var fly_to = options.fly_to;
            if (fly_to && isFinite(fly_to.x) && isFinite(fly_to.y)) {
                var ownerPos_x = death_emitter.ownerPos.x;
                var ownerPos_y = death_emitter.ownerPos.y;
                this._ANI.Tween({ v: 0 })
                    .to({
                    v: 1
                }, die_time)
                    .easing(Tween_1.default.Easing.Quartic.In)
                    .onComplete(remove_deatch_emitter)
                    .onUpdate(function () {
                    /**
                     * 动态获取，是的fly_to对象可以被外部动态修改或者使用getter属性动态获取
                     */
                    var fly_to = options.fly_to;
                    var res_point = common_1.LineAnimate([death_emitter.spawnPos], [{ x: fly_to.x - ownerPos_x, y: fly_to.y - ownerPos_y }], this.v)[0];
                    death_emitter.updateSpawnPos(res_point.x, res_point.y);
                })
                    .start();
            }
            else {
                setTimeout(remove_deatch_emitter, die_time);
            }
            death_emitter.cleanup();
            death_emitter.endScale = 0;
            death_emitter.emitterLifetime = 0.001 * die_time;
            var fps = 10;
            death_emitter.startAlpha = 2 / fps;
            death_emitter.endAlpha = 2 / fps;
            death_emitter.maxParticles = fps;
            death_emitter.frequency = 1 / fps / death_emitter.maxParticles;
            death_emitter.minLifetime = 1 / fps;
            death_emitter.maxLifetime = 1 / fps;
            death_emitter.emit = true;
            death_emitters.push(death_emitter);
            var light = this._lights.splice(index, 1)[0];
            var remover_light = this._lightsContainer.removeChild(light);
            var _pre_ligth_render_map = this._pre_ligth_render_map;
            for (var i in _pre_ligth_render_map) {
                var _i = ~~i;
                if (_i > index) {
                    _pre_ligth_render_map[_i - 1] = _pre_ligth_render_map[i];
                    _pre_ligth_render_map[i] = false;
                }
            }
            // console.log(death_emitter.ownerPos, light.position, remover_light)
            return this._particle_foods.splice(index, 1)[0];
        };
        /**移除某个点上的食物*/
        FoodContainer.prototype.removeParticleFood = function (x, y) {
            var _this = this;
            this._particle_foods.some(function (food, index) {
                if (food.x == x && food.y == y) {
                    _this.removeParticleFoodAt(index);
                    return true;
                }
            });
        };
        FoodContainer.prototype.clearParticleFood = function () {
            var _this = this;
            console.log("CLEAR!!!");
            //销毁食物
            this._particle_foods = [];
            //销毁食物·粒子效果
            this._emitters.forEach(function (emitter) {
                emitter.destroy();
            });
            this._emitters = [];
            //销毁食物·灯光效果
            var _pre_ligth_render_map = this._pre_ligth_render_map;
            this._lights.forEach(function (light, i) {
                if (_pre_ligth_render_map[i]) {
                    _this._lightsContainer.removeChild(light);
                }
            });
            this._lights = [];
            this._pre_ligth_render_map = {};
        };
        FoodContainer.prototype.eatFood = function (food) {
            if (food instanceof Food) {
            }
        };
        FoodContainer.prototype.updatePos = function (x, y, overflow) {
            if (overflow === void 0) { overflow = 10; }
            this.x = x;
            this.y = y;
            this.show_emitter_range_left = -x - overflow;
            this.show_emitter_range_right = this.show_emitter_range_left + common_1.VIEW_WIDTH + overflow + overflow;
            this.show_emitter_range_top = -y - overflow;
            this.show_emitter_range_bottom = this.show_emitter_range_top + common_1.VIEW_HEIGHT + overflow + overflow;
        };
        return FoodContainer;
    }(PIXI.Container));
    exports.FoodContainer = FoodContainer;
    exports.BodyFoodTexture = PIXI.Texture.fromImage("/images/food_particle.png");
    var FoodTexture = PIXI.Texture.fromImage("./js/lib/pixi.js-3.0.11/particle.png");
    var BodyFood = (function (_super) {
        __extends(BodyFood, _super);
        function BodyFood() {
            _super.call(this, exports.BodyFoodTexture);
            this.name = "肉";
            this.width = 10;
            this.height = 10;
            // var view = SVGGraphics.importFromSVG(`<circle cx="0" cy="0" r="5" stroke="#FFF"  stroke-width="1" fill="yellow" />`)._graphics
            // view.cacheAsBitmap = true;
            // this.addChild(view);
            this.effect.add_body = 1;
        }
        return BodyFood;
    }(Food));
    exports.BodyFood = BodyFood;
    var SpeedFood = (function (_super) {
        __extends(SpeedFood, _super);
        function SpeedFood() {
            _super.call(this);
            this.name = "速度";
            var view = SVGGraphics_1.default.importFromSVG("<circle cx=\"0\" cy=\"0\" r=\"5\" stroke=\"#FFF\"  stroke-width=\"1\" fill=\"blue\" />")._graphics;
            view.cacheAsBitmap = true;
            this.addChild(view);
            this.effect.add_speed = 2;
        }
        return SpeedFood;
    }(Food));
    exports.SpeedFood = SpeedFood;
    /**增加灵敏度的食物 */
    var SensitivityFood = (function (_super) {
        __extends(SensitivityFood, _super);
        function SensitivityFood() {
            _super.call(this);
            this.name = "灵敏";
            var view = SVGGraphics_1.default.importFromSVG("<circle cx=\"0\" cy=\"0\" r=\"5\" stroke=\"#FFF\"  stroke-width=\"1\" fill=\"green\" />")._graphics;
            view.cacheAsBitmap = true;
            this.addChild(view);
            this.effect.add_sensitivity = 5;
        }
        return SensitivityFood;
    }(Food));
    exports.SensitivityFood = SensitivityFood;
});
